<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts';
require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/utils'

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null,
      column: [],
      orderCountDatas: []
    }
  },
  mounted() {
    this.getPlaterform();
    // getOrderCount().then(res => {
    //   this.column = res.column,
    //     this.orderCountDatas = res.orderCountDatas
    // }).then(() =>{
    //   this.initChart()
    // })

    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHandler)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHandler)
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    getPlaterform() { 
         this.$http.get(`/api/data/countProduct`)
        .then(({ data: res }) => {

          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.column = res.data.column;
          this.orderCountDatas = res.data.orderCountDatas;

          this.initChart();
        })
        .catch(() => { });
    },
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')

      this.chart.setOption({
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        legend: {
          left: 'center',
          bottom: '10',
          data: this.column
        },
        calculable: true,
        series: [
          {
            name: '产品转换数据统计图',
            type: 'pie',
            roseType: 'radius',
            radius: [15, 95],
            center: ['50%', '38%'],
            data: this.orderCountDatas,
            animationEasing: 'cubicInOut',
            animationDuration: 2600
          }
        ]
      })
    }
  }
}
</script>


<style rel="stylesheet/scss" lang="scss" scoped>
.chart{
box-shadow: 4px 4px 40px rgba(0, 0, 0, .05);
background-color: #fff;
height: 359px !important;
}

</style>